package com.cash.loan.modular.cash.controller;

import com.cash.loan.core.base.controller.BaseController;
import com.cash.loan.core.util.DateUtil;
import com.cash.loan.modular.cash.result.PageResult;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.CashUserService;
import com.cash.loan.modular.cash.transfer.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户管理模块
 * @author Administrator
 * @Date 2018-07-11 11:51
 */
@Controller
@RequestMapping(value = "/cash/user")
@Api(description = "用户管理")
public class CashUserController extends BaseController{

    private static final String PREFIX = "/cash/user";

    @Autowired
    private CashUserService userService;

    /**
     * 进入用户列表页
     * @return
     */
    @RequestMapping(value = "/listPage")
    public String toUserPage() {
        return PREFIX + "/user_list.html";
    }

    @ApiOperation("用户列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse getUserList(@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                      @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                      @RequestParam(value = "keyword",required = false) String keyword,
                                      @RequestParam(value = "startDate",required = false) String startDate,
                                      @RequestParam(value = "endDate",required = false) String endDate) {

        Date sDate = null;

        Date eDate = null;

        if(StringUtils.isNoneBlank(startDate)) {
            sDate = DateUtil.parseDate(startDate);
        }

        if(StringUtils.isNoneBlank(endDate)) {
            eDate = DateUtil.parseDate(endDate);
        }

        PageResult<UserVo> pageResult = userService.getUserList(currentPage,pageSize,keyword,sDate,eDate);

        return ServerResponse.getSuccess("success",pageResult);
    }

    @ApiOperation("用户删除")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse deleteUser(@RequestParam(value = "id") Integer id) {
        Boolean result = userService.deleteUser(id);
        if(result) {
            return ServerResponse.getSuccess("删除成功");
        } else {
            return ServerResponse.getErrorMessage("删除失败");
        }
    }

    /**
     * 下载文件
     * @return
     */
    @RequestMapping(value = "/excel")
    public ResponseEntity<byte[]> outputUserExcel(@RequestParam(value = "startDate",required = false) String startDate,
                                                  @RequestParam(value = "endDate",required = false) String endDate) {
        Date sDate = null;

        Date eDate = null;

        if(StringUtils.isNoneBlank(startDate)) {
            sDate = DateUtil.parseDate(startDate);
        }

        if(StringUtils.isNoneBlank(endDate)) {
            eDate = DateUtil.parseDate(endDate);
        }
        String filePath = userService.getUserInfoToExcel(sDate,eDate);
        return renderFile("用户信息.xlsx",filePath);
    }
}
